import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_roujiaosuo/flutter_bloc/requests/repository/home_repository.dart';
import './bloc.dart';

class HomeBloc extends Bloc<HomeEvent, HomeState> {
  final HomeRepository homeRepository;
  HomeBloc({@required this.homeRepository})
      : assert(homeRepository != null),
        super();
  @override
  HomeState get initialState => InitialHomeState();

  @override
  Stream<HomeState> mapEventToState(
    HomeEvent event,
  ) async* {
    // TODO: Add Logic
    if (event is FetchHomeTopDataEvent) {
      yield LoadingHomeState();
      try {
        yield LoadedHomeState(homeTopModel: await homeRepository.getHomeData());
      } catch (e) {
        yield ErrorHomeState();
      }
    } else if(event is HomeScrollViewOffsetEvent){
      yield HomeScrollOffsetState(offset: event.scrollOffset);
    }
  }
}
